<!-- 元数据编辑对话框 -->
<template>
  <CommonDialog v-model="visible" title="批量修改" :width="640"
    @submit="submit" @cancel="cancel">
    <Form ref="formEl" :model="formData" :label-width="140">
      <FormItem label="禁用导入导出">
        <Switch v-model="formData.impfield" size="large" :true-value="1" :false-value="0">
          <template #open>
            <span>开启</span>
          </template>
          <template #close>
            <span>关闭</span>
          </template>
        </Switch>
      </FormItem>

      <FormItem label="显示位置">
        <CheckboxGroup v-model="formData.viewtype">
          <Checkbox v-for="item in SHOW_ADDRESS_OPTIONS" :key="item.value" :label="item.value">
            {{ item.label }}</Checkbox>
        </CheckboxGroup>
      </FormItem>
    </Form>
  </CommonDialog>
</template>

<script setup>
import { ref } from 'vue'
import { Message } from 'view-ui-plus'
import { SHOW_ADDRESS_OPTIONS } from '@/util/conf'

import { metadata } from '@/api/modules/modelSetting'

const emit = defineEmits(['cancel', 'submit'])

// 是否打开弹窗
const visible = ref(true)

// 加载状态
const loading = ref(false)

// 元数据ID
const metadataIds = ref(undefined)
// 表单数据
const formData = ref({
  impfield: 0, //禁用导入导出
  viewtype: [], //显示位置
})
/**
 * 提交表单
 */
const submit = async () => {
  loading.value = true

  try {
    await metadata.propertiesBatedit({
      data: {
        ...formData.value,
        viewtype: formData.value.viewtype.toString()
      },
      query: {
        ids: metadataIds.value
      }
    })

    Message.success('操作成功')
    emit('submit')
  } catch (error) {
    console.error(error)
  }

  loading.value = false
}

/**
 * 取消表单
 */
const cancel = () => {
  visible.value = false
  emit('cancel')
}

/**
 * 初始化对话框
 */
const initDialog = ids => {
  // 显示对话框
  visible.value = true

  metadataIds.value = ids
  console.log(metadataIds)
}

defineExpose({
  initDialog
})
</script>

<style scoped lang="scss">
.ivu-input-wrapper,
.ivu-select {
  width: 240px;
}
::deep(.ivu-form-item-label) {
  text-align: right;
}
</style>